﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Twodi.Interfaces;
using Twodi.Arguments;

namespace Twodi.Implemented
{
    /// <summary>
    /// Componente capaz de se renderizar.
    /// </summary>
    public class RenderManaged : Component, IRenderManaged
    {
        /// <summary>
        /// Spritebatch para render.
        /// </summary>
        private SpriteBatch spriteBatch;

        /// <summary>
        /// Construtor padrão.
        /// </summary>
        /// <param name="spriteBatch">Spritebatch para render.</param>
        /// <param name="game"></param>
        public RenderManaged(Game game)
            : base(game)
        {

        }

        public override void Draw(GameTime gameTime)
        {
            if (!Visible)
                return;

            onPreRender(new RenderEventArgs<SpriteBatch>(SpriteBatch));
            this.Render(gameTime);
            onPosRender(new RenderEventArgs<SpriteBatch>(SpriteBatch));
            base.Draw(gameTime);
        }

        public virtual void Render(GameTime gameTime)
        {

        }

        /// <summary>
        /// Spritebatch para render.
        /// </summary>
        public SpriteBatch SpriteBatch
        {
            get { return this.spriteBatch; }
            set
            {
                this.spriteBatch = value;
                onSpriteBatchChanged(EventArgs.Empty);
            }
        }

        /// <summary>
        /// SpriteBatch é valido.
        /// </summary>
        public bool ValidSpriteBatch
        {
            get
            {
                return SpriteBatch != null;
            }
        }

        /// <summary>
        /// Evento invocado quando SpriteBatch é alterado.
        /// </summary>
        public event EventHandler<EventArgs> SpriteBatchChanged;

        /// <summary>
        /// Evento invocado antes de ser iniciado o Draw();
        /// </summary>
        public event EventHandler<RenderEventArgs<SpriteBatch>> PreRender;

        /// <summary>
        /// Evento invocado após ser finalizado o Draw();
        /// </summary>
        public event EventHandler<RenderEventArgs<SpriteBatch>> PosRender;

        /// <summary>
        /// Evento invocado quando SpriteBatch é alterado.
        /// </summary>
        protected virtual void onSpriteBatchChanged(EventArgs e)
        {
            if (SpriteBatchChanged != null)
            {
                SpriteBatchChanged(this, e);
            }
        }

        /// <summary>
        /// Método invocado antes de ser iniciado o Draw();
        /// </summary>
        protected virtual void onPreRender(RenderEventArgs<SpriteBatch> e)
        {
            if (PreRender != null)
            {
                PreRender(this, e);
            }
        }

        /// <summary>
        /// Método invocado após de ser finalizado o Draw();
        /// </summary>
        protected virtual void onPosRender(RenderEventArgs<SpriteBatch> e)
        {
            if (PosRender != null)
            {
                PosRender(this, e);
            }
        }
    }
}
